/*
 * Date          Author     Note
 * 2015.04.23    舞者无罪         摇床方向设置
 * 2015.04.25    wzwz       增加方向键
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "rock_drt.h"
#include "com.h"

/*
 *
 */
static void CreateRockDrtInterface(HWND hWnd)
{
    const char *sc[8] = {
            "L", "L *", "L +", "L -",
            "R", "R *", "R +", "R -",
    };
    int i = 0;
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    for(i = 0; i < 8; ++i)
    {
        CreateWindow(MH_EDIT_NAME,
            sc[i],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + i,
            50 + (i >> 2) * 140,
            60 + (i & 3) * 30,
            60,
            20,
            hWnd, data.act.des == i ? MH_EDIT_BORDER_GREY : 0);
    }
}
/*
 *
 */
static int GetRockIndex(uchar drt, uchar flag)
{
    int ret = 0;
    if(drt == ROCK_LEFT)//left
    {
        if(flag & 1)
        {
            ret = 1;
        }
        else
        {
            ret = 0;
        }
    }
    else
    {
        if(flag & 1)
        {
            ret = 5;
        }
        else
        {
            ret = 4;
        }
    }
    return ret;
}
/*
 *
 */
static void GetRockOfIndex(ST_ROCK *prock, int index)
{
    prock->flag = 0;
    prock->drt = ROCK_RIGHT;
    switch(index)
    {
    case 0:
    case 2:
    case 3:
        prock->drt = ROCK_LEFT;
        break;
    case 1:
        prock->drt = ROCK_LEFT;
        prock->flag = TRUE;
        break;
    case 4:
    case 6:
    case 7:
        prock->drt = ROCK_RIGHT;
        break;
    case 5:
        prock->drt = ROCK_RIGHT;
        prock->flag = TRUE;
        break;
    }
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, ushort nID)
{
    if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 8)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        int index = nID - IDC_EDIT1;
        if(data.act.des != index)
        {
            HWND hTmp = GetDlgItem(hWnd, data.act.des + IDC_EDIT1);
            SetEditBorder(hTmp, 0);
            InvalidateRect(hTmp, NULL, TRUE);
            hTmp = GetDlgItem(hWnd, nID);
            SetEditBorder(hTmp, MH_EDIT_BORDER_GREY);
            InvalidateRect(hTmp, NULL, TRUE);
            data.act.des = index;
            SetWindowAdditionalData(hWnd, data.value);
        }
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
    {
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_ENTER)
    {
        WND_EXTRA_DATA data;
        data.value = GetWindowAdditionalData(hWnd);
        if(data.act.src != data.act.des)
        {
            ST_ROCK rock;
            GetRockOfIndex(&rock, data.act.des);
            uchar *pact = (uchar*)&rock;
            SendMessage(GetHosting(hWnd), MSG_SETDATA, (*pact << 16) | ID_ROCK, 0);
        }
        PostMessage(hWnd, MSG_CLOSE, 0, 0);
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 4)
        {
            nID += 4;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetFocusChild(hWnd);
        if(nID > IDC_EDIT1 + 3 && nID < IDC_EDIT1 + 8)
        {
            nID -= 4;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetFocusChild(hWnd);
        if(nID > IDC_EDIT1 && nID < IDC_EDIT1 + 8)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetFocusChild(hWnd);
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 7)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
}
/*
 *
 */
static int RockDrtWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateRockDrtInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting > 0)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        break;
    case MSG_CHAR:
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_TIME]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateRockDrtWindow(HWND hHosting, uchar drt, uchar flag)
{
    WND_EXTRA_DATA data;
    int index = GetRockIndex(drt, flag);
    data.act.src = index;
    data.act.des = data.act.src;

    int x = (CX_SCREEN - CX_TIME_BMP) / 2;
    int y = (CY_SCREEN - CY_TIME_BMP) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(580);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = RockDrtWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_TIME_BMP;
    CreateInfo.by = y + CY_TIME_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1 + index));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
